package com.tianbo.elfii.client.common;

import java.io.*;
import java.sql.* ;
import java.net.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.*;

/**  <pre>
     <b>类功能描述：</b>
     完成 Config-系统恢复 部分功能要求。
     </pre>
     @version 1.0.1
     @author 高伟 
*/

public class ElfiiCommonConfigRestore {
    JPanel configPanel;
    JPanel itemPanel;

    int infoBgStartX0;
    int infoBgStartY0;

    int dataChoiceIndex;

    boolean isAboutMachineSelected=false;
    boolean isWifiSettingSelected=false;

    JFrame mainFrame;
    ElfiiCommonRunInfo runInfo;

    ItemButtonPanel restoreContentsSettingsPanel;
    ItemButtonPanel restoreSettingsPanel;


    final int Id_restoreContentsSettingsPanel=1;
    final int Id_restoreSettingsPanel=2;

    int selectedId;

    JDialog restoreContentSettingsWindow ;
    JDialog restoreSettingsWindow ;

    public void setRunInfo(ElfiiCommonRunInfo runInfo){
        this.runInfo=runInfo;
        this.mainFrame=runInfo.getMainFrame();
    }

    public void dealBackButton(){
        mainFrame.getContentPane().removeAll();
        ElfiiCommonConfig config = new ElfiiCommonConfig();
        config.setRunInfo(runInfo);
        JPanel p=config.createViewPanel();
        mainFrame.getContentPane().setLayout(new BorderLayout());
        mainFrame.getContentPane().add(p,BorderLayout.CENTER);
        mainFrame.validate();
    }

    public void dealOkButton(){

        if (selectedId==Id_restoreContentsSettingsPanel) {

            JButton restoreYesButton=new JButton();
            ImageIcon backIcon = new ImageIcon("resource-common/images/login/sssss06.png","");
            restoreYesButton.setBackground(Color.white);
            restoreYesButton.setBorderPainted(false);
            restoreYesButton.setFocusPainted(false);
            restoreYesButton.setIcon(backIcon);
            restoreYesButton.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        restoreContentSettingsWindow.dispose();
                    }
                });

            restoreYesButton.setBackground(Color.darkGray);
            restoreYesButton.setContentAreaFilled(false);

            JButton restoreCancelButton=new JButton();
            ImageIcon backIcon2 = new ImageIcon("resource-common/images/login/sssss07.png","");
            restoreCancelButton.setBackground(Color.white);
            restoreCancelButton.setBorderPainted(false);
            restoreCancelButton.setFocusPainted(false);
            restoreCancelButton.setIcon(backIcon2);
            restoreCancelButton.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        restoreContentSettingsWindow.dispose();
                    }
                });

            restoreCancelButton.setContentAreaFilled(false);

            restoreContentSettingsWindow=new JDialog(ElfiiCommonRunInfo.getRunInfo().getMainFrame(),true);
            restoreContentSettingsWindow.setUndecorated(true);		  
            restoreContentSettingsWindow.setBackground(null);

            int x0=(int) mainFrame.getLocationOnScreen().getX();
            int y0=(int) mainFrame.getLocationOnScreen().getY();

            restoreContentSettingsWindow.setBounds(x0,y0,1024,768);
            restoreContentSettingsWindow.getContentPane().setLayout(null);

            RestoreContentSettingsWindowPanel p= new RestoreContentSettingsWindowPanel();
            p.setLayout(null);
            p.add(restoreYesButton);
            restoreYesButton.setBounds(350,450,121,43);
            p.add(restoreCancelButton);
            restoreCancelButton.setBounds(550,450,121,43);
            restoreContentSettingsWindow.getContentPane().add(p);
            p.setBounds(0,0,1024,768);

            restoreContentSettingsWindow.validate();
            restoreContentSettingsWindow.setVisible(true);
        }

        if (selectedId==Id_restoreSettingsPanel) {

            JButton restoreYesButton=new JButton();
            ImageIcon backIcon = new ImageIcon("resource-common/images/login/sssss06.png","");
            restoreYesButton.setBackground(Color.white);
            restoreYesButton.setBorderPainted(false);
            restoreYesButton.setFocusPainted(false);
            restoreYesButton.setIcon(backIcon);
            restoreYesButton.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        restoreSettingsWindow.dispose();
                    }
                });

            restoreYesButton.setBackground(Color.darkGray);
            restoreYesButton.setContentAreaFilled(false);

            JButton restoreCancelButton=new JButton();
            ImageIcon backIcon2 = new ImageIcon("resource-common/images/login/sssss07.png","");
            restoreCancelButton.setBackground(Color.white);
            restoreCancelButton.setBorderPainted(false);
            restoreCancelButton.setFocusPainted(false);
            restoreCancelButton.setIcon(backIcon2);
            restoreCancelButton.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        restoreSettingsWindow.dispose();
                    }
                });

            restoreCancelButton.setContentAreaFilled(false);

            restoreSettingsWindow=new JDialog(ElfiiCommonRunInfo.getRunInfo().getMainFrame(),true);
            restoreSettingsWindow.setUndecorated(true);		  
            restoreSettingsWindow.setBackground(null);

            int x0=(int) mainFrame.getLocationOnScreen().getX();
            int y0=(int) mainFrame.getLocationOnScreen().getY();

            restoreSettingsWindow.setBounds(x0,y0,1024,768);
            restoreSettingsWindow.getContentPane().setLayout(null);

            RestoreSettingsWindowPanel p= new RestoreSettingsWindowPanel();
            p.setLayout(null);
            p.add(restoreYesButton);
            restoreYesButton.setBounds(350,450,121,43);
            p.add(restoreCancelButton);
            restoreCancelButton.setBounds(550,450,121,43);
            restoreSettingsWindow.getContentPane().add(p);
            p.setBounds(0,0,1024,768);

            restoreSettingsWindow.validate();
            restoreSettingsWindow.setVisible(true);
        }

    }

    class RestoreContentSettingsWindowPanel extends JPanel {

        BufferedImage infoImage;
        BufferedImage bgImg;

        public RestoreContentSettingsWindowPanel(){
            File f;
            try {
                f=new File("resource-common/images/config/icons/tb.png");
                infoImage=ImageIO.read(f);

                int x0=(int) mainFrame.getLocationOnScreen().getX();
                int y0=(int) mainFrame.getLocationOnScreen().getY();
                Robot rbt = new Robot();
                bgImg=rbt.createScreenCapture(new Rectangle(x0,y0,1024,768));

            } catch(Exception e) {System.out.print(e);}
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));   
            g2.drawImage(bgImg,null,0,0);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));   

            int rectX0=242,rectY0=220;
            int rectWidth=530,rectHeight=280;

            g2.setColor(Color.white);
            g2.fillRoundRect(rectX0,rectY0,rectWidth,rectHeight,10,10);
            g2.setColor(Color.black);
            g2.fillRoundRect(rectX0+5,rectY0+5,rectWidth-10,rectHeight-10,10,10);
            g2.drawImage(infoImage,null,rectX0+30,rectY0+20);
            g2.setColor(Color.gray);
            g2.drawLine(rectX0+20,rectY0+85,rectX0+rectWidth-20,rectY0+85);
            g2.fillRect(rectX0+5,rectY0+rectHeight-55,rectWidth-10,50);

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,30));
            g2.setColor(Color.white);
            g2.drawString("还原所有内容和设置",rectX0+115,rectY0+60);
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,20));
            g2.drawString("此操作会清除电脑上所有设置",rectX0+110,rectY0+150);

        }
        public Dimension getPreferredSize() {
            return (new Dimension(300,100));
        }
    }

    class RestoreSettingsWindowPanel extends JPanel {

        BufferedImage infoImage;
        BufferedImage bgImg;

        public RestoreSettingsWindowPanel(){
            File f;
            try {
                f=new File("resource-common/images/config/icons/tb.png");
                infoImage=ImageIO.read(f);

                int x0=(int) mainFrame.getLocationOnScreen().getX();
                int y0=(int) mainFrame.getLocationOnScreen().getY();
                Robot rbt = new Robot();
                bgImg=rbt.createScreenCapture(new Rectangle(x0,y0,1024,768));

            } catch(Exception e) {System.out.print(e);}
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));   
            g2.drawImage(bgImg,null,0,0);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));   

            int rectX0=242,rectY0=220;
            int rectWidth=530,rectHeight=280;

            g2.setColor(Color.white);
            g2.fillRoundRect(rectX0,rectY0,rectWidth,rectHeight,10,10);
            g2.setColor(Color.black);
            g2.fillRoundRect(rectX0+5,rectY0+5,rectWidth-10,rectHeight-10,10,10);
            g2.drawImage(infoImage,null,rectX0+30,rectY0+20);
            g2.setColor(Color.gray);
            g2.drawLine(rectX0+20,rectY0+85,rectX0+rectWidth-20,rectY0+85);
            g2.fillRect(rectX0+5,rectY0+rectHeight-55,rectWidth-10,50);

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,30));
            g2.setColor(Color.white);
            g2.drawString("还原所有设置",rectX0+115,rectY0+60);
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,20));
            g2.drawString("此操作会清除电脑上所有数据",rectX0+110,rectY0+150);

        }
        public Dimension getPreferredSize() {
            return (new Dimension(300,100));
        }
    }


    public void createConfigPanel(){

        JButton aboutMachineButton=new JButton("系统还原");
        aboutMachineButton.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,15));
        aboutMachineButton.setBackground(Color.gray);
        aboutMachineButton.setForeground(Color.white);
        aboutMachineButton.setBorderPainted(false);
        aboutMachineButton.setFocusPainted(false);
        aboutMachineButton.setHorizontalAlignment(SwingConstants.LEFT);
        aboutMachineButton.setModel(new DefaultButtonModel() {
                public boolean isPressed(){
                    return false; 
                }
            }
            ) ;


        restoreContentsSettingsPanel=new ItemButtonPanel("还原所有设置","将应用程序恢复还原至默认设置");
        restoreContentsSettingsPanel.setFocusable(true);
        restoreContentsSettingsPanel.setBackground(Color.black);
        restoreContentsSettingsPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_restoreContentsSettingsPanel);
                }
            }
            );

        restoreSettingsPanel=new ItemButtonPanel("还原所有内容和设置","清除电脑上的所有设置");
        restoreSettingsPanel.setFocusable(true);
        restoreSettingsPanel.setBackground(Color.black);
        restoreSettingsPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    setSelectButton(Id_restoreSettingsPanel);
                }
            }
            );

        configPanel=new JPanel();
        configPanel.setBackground(Color.black);
        configPanel.setLayout(null);

        configPanel.add(aboutMachineButton);
        aboutMachineButton.setBounds(0,10,1024,25);

        configPanel.add(restoreContentsSettingsPanel);
        restoreContentsSettingsPanel.setBounds(0,35,1024,67);
        configPanel.add(restoreSettingsPanel);
        restoreSettingsPanel.setBounds(0,100,1024,67);


    }

    public void setSelectButton(int selectedId){
        this.selectedId=selectedId;
        restoreContentsSettingsPanel.setSelected(false);
        restoreSettingsPanel.setSelected(false);

        if (selectedId==Id_restoreContentsSettingsPanel)
            restoreContentsSettingsPanel.setSelected(true);
        if (selectedId==Id_restoreSettingsPanel)
            restoreSettingsPanel.setSelected(true);

    }

    class ItemButtonPanel extends JPanel {
        BufferedImage selectedBgImage;

        BufferedImage lineImage;

        BufferedImage img2;

        BufferedImage gaoImg;

        String str1;
        String str2;
        boolean isSelected;
        public ItemButtonPanel(String str1,
                               String str2) {
            this.str1=str1;
            this.str2=str2;
            File f=null;
            try {
                f=new File("resource-common/images/config/icons/tc_butbg.png");
                selectedBgImage=ImageIO.read(f);
                f=new File("resource-common/images/config/icons/tb_bg.png");
                img2=ImageIO.read(f);
                f=new File("resource-common/images/config/icons/line.png");
                lineImage=ImageIO.read(f);
            } catch(Exception e) {System.out.print(e+"   "+f);}
        }

        public void setSelected(boolean isSelected){
            this.isSelected=isSelected;
            repaint();
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setColor(Color.black);
            g2.fillRect(0,0,1024,58);
            if (isSelected) {
                g2.drawImage(selectedBgImage,null,10,0);
                //g2.drawImage(img2,null,0,0);
                //g2.drawImage(selectedImage,null,10,0);
            }else{
                //g2.drawImage(img2,null,10,0);
                //g2.drawImage(unSelectedImage,null,10,0);
            }

            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,22));
            g2.setColor(Color.white);
            g2.drawString(str1,20,25);  
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.PLAIN,16));
            g2.drawString(str2,20,50);  
            g2.drawImage(lineImage,null,0,55);
        }

        public Dimension getPreferredSize() {
            return (new Dimension(1024,66));
        }
    }

    public JPanel createViewPanel(){
        JPanel p = new JPanel(new BorderLayout()) ;
        JButton okButton=new JButton();
        okButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealOkButton();
                }
            });

        JButton backButton=new JButton();
        backButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealBackButton();
                }
            });

        JButton homeButton=runInfo.createHomeButton();
        JPanel topPanel=ElfiiCommonUtil.createTopPanel("系统配置",backButton,homeButton);
        JPanel bottomPanel=ElfiiCommonUtil.createBottomPanelLevelSelect(okButton);
        createConfigPanel();
        p.setLayout(null);
        p.setBackground(Color.black);
        p.add(topPanel);
        topPanel.setBounds(0,0,1024,100);
        p.add(bottomPanel);
        bottomPanel.setBounds(0,668,1024,100);
        p.add(configPanel);
        configPanel.setBounds(0,101,1024,560);

        return p;
    }
}
